var options = {
  valueNames: [ 'id', 'fname', 'lname', 'birthday', 'sex', 'login', 'email', 'status', 'role']
};


var users = new List('usersTable', options);

var idField = $('#id-field'),
    nameField = $('#name-field'),
    ageField = $('#age-field'),
    cityField = $('#city-field'),
    
    fname = $('#fname-field'),
    lname = $('#lname-field'),
    birthday = $('#birthday-field'),
    sex = $('#sex-field'),
    login = $('#login-field'),
    email = $('#email-field'),
    status = $('#status-field'),
    role = $('#role-field'),

    
    addBtn = $('#add-btn'),
    editBtn = $('#edit-btn').hide(),
    removeBtns = $('.remove-item-btn'),
    editBtns = $('.edit-item-btn');


refreshCallbacks();

addBtn.click(function() {
  users.add({
    id: Math.floor(Math.random()*110000),
    fname: fname.val(),
    lname: lname.val(),
    birthday: birthday.val(),
    sex: sex.val(),
    login: login.val(),
    email: email.val(),
    status: status.val(),
    role: role.val()
    
  });
  clearFields();
  refreshCallbacks();
});


function refreshCallbacks() {
  removeBtns = $(removeBtns.selector);
  editBtns = $(editBtns.selector);
  
  removeBtns.click(function() {
    var itemId = $(this).closest('tr').find('.id').text();
    users.remove('id', itemId);
  });
  
  editBtns.click(function() {
    var itemId = $(this).closest('tr').find('.id').text();
    var itemValues = users.get('id', itemId)[0].values();
    idField.val(itemValues.id);
    fname.val(itemValues.fname);
    lname.val(itemValues.lname);
    birthday.val(itemValues.birthday);
    sex.val(itemValues.sex);
    login.val(itemValues.login);
    email.val(itemValues.email);
    status.val(itemValues.status);
    role.val(itemValue.role);
    editBtn.show();
    addBtn.hide();
  });
  
}

function clearFields() {
  fname.val('');
  lname.val('');
  birthday.val('');
  sex.val('');
  login.val('');
  email.val('');
  status.val('');
  role.val('');
}

function removeUser(userId) {
	$.ajax({
		type : "POST",
		url : "removeUser",
		data : "userId=" + userId,
	});
};

var userIdForChangeRole;

$(document).on("click", "#change", function () {   
    userIdForChangeRole = $(this).closest('tr').find('.id').text();
	document.getElementById("userLoginForChangeRole").innerHTML = $(this).closest('tr').find('.login').text();	
});



$(document).on("click", "#saveNewRole", function () { 
	var newRole = $(".modal-body #newRole").val();
	params = {
			userId : userIdForChangeRole,
			roleId : newRole
		};
	$.ajax({
		type : "POST",
		url : "changeRole",
		data : params,
		success : function(res) {
			if (newRole == 1) {
				$("tr[id=" + userIdForChangeRole + "]").find(".role").text("Administrator");
			} else if (newRole ==2) {
				$("tr[id=" + userIdForChangeRole + "]").find(".role").text("Manager");
			} else if (newRole ==3) {
				$("tr[id=" + userIdForChangeRole + "]").find(".role").text("User");
			} else if (newRole ==4) {
				$("tr[id=" + userIdForChangeRole + "]").find(".role").text("Researcher");
			}
			$('#changeRoleModal').modal('hide');
		}
	});	
});
